package com.ct.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.ct.entity.enums.PageSize;
import com.ct.entity.po.Approval;
import com.ct.entity.po.Reimbursement;
import com.ct.entity.query.ReimbursementQuery;
import com.ct.entity.query.SimplePage;
import com.ct.entity.vo.PaginationResultVO;
import com.ct.mappers.ReimbursementMapper;
import com.ct.service.ReimbursementService;
import com.ct.service.client.ApprovalClient;
import com.ct.utils.StringTools;

import jakarta.annotation.Resource;

/**
 * 报销表 业务接口实现
 */
@Service("reimbursementService")
public class ReimbursementServiceImpl implements ReimbursementService {

    @Resource
    private ReimbursementMapper<Reimbursement, ReimbursementQuery> reimbursementMapper;

    // 这个就是你想使用其他service的具体声明
    @Resource
    private ApprovalClient approvalClient;

    /**
     * 根据条件查询列表
     */
    @Override
    public List<Reimbursement> findListByParam(ReimbursementQuery param) {
        return this.reimbursementMapper.selectList(param);
    }

    /**
     * 根据条件查询列表
     */
    @Override
    public Integer findCountByParam(ReimbursementQuery param) {
        return this.reimbursementMapper.selectCount(param);
    }

    /**
     * 分页查询方法
     */
    @Override
    public PaginationResultVO<Reimbursement> findListByPage(ReimbursementQuery param) {
        int count = this.findCountByParam(param);
        int pageSize = param.getPageSize() == null ? PageSize.SIZE15.getSize() : param.getPageSize();

        SimplePage page = new SimplePage(param.getPageNo(), count, pageSize);
        param.setSimplePage(page);
        List<Reimbursement> list = this.findListByParam(param);
        PaginationResultVO<Reimbursement> result = new PaginationResultVO(count, page.getPageSize(), page.getPageNo(),
                page.getPageTotal(), list);
        return result;
    }

    /**
     * 新增
     */
    @Override
    public Integer add(Reimbursement bean) {
        Approval approval = this.approvalClient.getApprovalByTravelId(String.valueOf(bean.getTravelId()));
        this.approvalClient.updateApprovalByTravelId(String.valueOf(bean.getTravelId()));
        return this.reimbursementMapper.insert(bean);
    }

    /**
     * 批量新增
     */
    @Override
    public Integer addBatch(List<Reimbursement> listBean) {
        if (listBean == null || listBean.isEmpty()) {
            return 0;
        }
        return this.reimbursementMapper.insertBatch(listBean);
    }

    /**
     * 批量新增或者修改
     */
    @Override
    public Integer addOrUpdateBatch(List<Reimbursement> listBean) {
        if (listBean == null || listBean.isEmpty()) {
            return 0;
        }
        return this.reimbursementMapper.insertOrUpdateBatch(listBean);
    }

    /**
     * 多条件更新
     */
    @Override
    public Integer updateByParam(Reimbursement bean, ReimbursementQuery param) {
        StringTools.checkParam(param);
        return this.reimbursementMapper.updateByParam(bean, param);
    }

    /**
     * 多条件删除
     */
    @Override
    public Integer deleteByParam(ReimbursementQuery param) {
        StringTools.checkParam(param);
        return this.reimbursementMapper.deleteByParam(param);
    }

    /**
     * 根据ReimbursementId获取对象
     */
    @Override
    public Reimbursement getReimbursementByReimbursementId(Integer reimbursementId) {
        return this.reimbursementMapper.selectByReimbursementId(reimbursementId);
    }

    /**
     * 根据ReimbursementId修改
     */
    @Override
    public Integer updateReimbursementByReimbursementId(Reimbursement bean, Integer reimbursementId) {
        return this.reimbursementMapper.updateByReimbursementId(bean, reimbursementId);
    }

    /**
     * 根据ReimbursementId删除
     */
    @Override
    public Integer deleteReimbursementByReimbursementId(Integer reimbursementId) {
        return this.reimbursementMapper.deleteByReimbursementId(reimbursementId);
    }

    @Override
    public List<Reimbursement> getReimbursementByTravelIdList(List<Integer> travelIdList) {
        ArrayList<Reimbursement> reimbursementList = new ArrayList<>();
        for (Integer travelId : travelIdList) {
            ReimbursementQuery query = new ReimbursementQuery();
            query.setTravelId(travelId);
            Reimbursement reimbursement = this.findListByParam(query).get(0);
            if (null != reimbursement) {
                reimbursementList.add(reimbursement);
            }
        }
        return reimbursementList;
    }

    /**
     * 根据travelId查询一个Reimbursement,(这里的reimbursementMapper是null不知道为啥)
     * 
     * @param travelId
     * @return
     */
    @Override
    public Reimbursement getReimbursementByTravelId(Integer travelId) {
        return this.reimbursementMapper.selectByTravelId(travelId);
    }
}